<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="fieldList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="表名" align="center" prop="table_code" />
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="代码" align="center" prop="code" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="最小长度" align="center" prop="max_length" />
      <el-table-column label="最大长度" align="center" prop="min_length" />
      <el-table-column label="小数点位" align="center" prop="scale" />
      <el-table-column label="为空" align="center" prop="nullable">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.nullable"/>
        </template>
      </el-table-column>
      <el-table-column label="验证规则" align="center" prop="rules" />
      <el-table-column label="默认值" align="center" prop="defaults" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:field:edit']"
          >修改</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="pager.page"
      :limit.sync="pager.limit"
      @pagination="getList"
    />
    <!-- 添加或修改模型字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表名" prop="table_code">
          <el-input v-model="form.table_code" placeholder="请输入表名" />
        </el-form-item>
        <el-form-item label="代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入代码" />
        </el-form-item>
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="最小长度" prop="max_length">
          <el-input v-model="form.max_length" placeholder="请输入最小长度" />
        </el-form-item>
        <el-form-item label="最大长度" prop="min_length">
          <el-input v-model="form.min_length" placeholder="请输入最大长度" />
        </el-form-item>
        <el-form-item label="小数点位" prop="scale">
          <el-input v-model="form.scale" placeholder="请输入小数点位" />
        </el-form-item>
        <el-form-item label="为空" prop="nullable">
          <el-radio-group v-model="form.nullable">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="验证规则" prop="rules">
          <el-input v-model="form.rules" placeholder="请输入验证规则" />
        </el-form-item>
        <el-form-item label="默认值" prop="defaults">
          <el-input v-model="form.defaults" placeholder="请输入默认值,前边不要有@,会自动补充" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { apiGet, apiPost, apiPut, apiDs } from "@/api/openapi";
export default {
  name: "Field",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      modelId: null,
      // 模型字段表格数据
      fieldList: [],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        model_id: null,
        table_code: null,
        type: null,
        code: null,
        name: null,
        nullable: null,
        "@table": "api_field",
        "@exp": {
      // 默认值模糊搜索
      name: 'lk',
        }
      },
      pager: {
        page: 1,
        limit: 10,
        order: "id desc",
      },
      // 导入参数
      upload: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        model_id: [
          { required: true, message: "模型不能为空", trigger: "blur" }
        ],
        table_code: [
          { required: true, message: "表名不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "类型不能为空", trigger: "change" }
        ],
        code: [
          { required: true, message: "代码不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" }
        ],
        max_length: [
          { required: true, message: "最小长度不能为空", trigger: "blur" }
        ],
        nullable: [
          { required: true, message: "为空不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    const modelId = this.$route.params && this.$route.params.modelId;
    this.modelId = modelId;
    this.queryParams.model_id = modelId;
    this.getList();
  },
  methods: {
    /** 查询模型字段列表 */
    getList() {
      this.loading = true;
      let params = { ...this.queryParams, '@pager': this.pager};
      apiGet(params).then(response => {
        this.fieldList = response.data.rows;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        model_id: null,
        table_code: null,
        type: null,
        code: null,
        name: null,
        max_length: null,
        min_length: null,
        scale: null,
        nullable: null,
        rules: null,
        defaults: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.pager.page = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
        let data = {"@table":"api_field", id:id};
      apiGet(data).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模型字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let data = {
            'api_field': this.form,
           '@tags': ['api_field']
          };
          apiPut(data).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });           
        }
      });
    }
  }
};
</script>
